<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期" prop="dateInfo">
        <el-date-picker clearable v-model="queryParams.dateInfo" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择日期" :default-value="getDefaultDate()">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="渠道编号" prop="channelCode">
        <el-input v-model="queryParams.channelCode" placeholder="请输入渠道编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="渠道名称" prop="channelName" class="single-line-label">
        <span class="label-spacing"></span>
        <el-select v-readonlyRemove v-model="selectedChannelNames" multiple clearable placeholder="请选择渠道名称" filterable
          @change="updateChannelNames">
          <el-option v-for="(item, i) in extData.channelNameList || []" :key="i" :label="item.key"
            :value="item.key"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataCountList">

      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="日期" align="center" prop="dateInfo" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateInfo, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <!-- <el-table-column label="渠道编号" align="center" prop="channelCode" />
      <el-table-column label="渠道名称" align="center" prop="channelName" /> -->
      <el-table-column label="渠道信息" align="center" prop="channelName" width="250">
        <template slot-scope="scope">
          [{{ scope.row.channelCode }}] {{ scope.row.channelName }}
          <span :style="{ color: scope.row.type === 'APIB' ? 'red' : scope.row.type === 'APIS' ? 'blue' : 'green' }">
            [{{ scope.row.type }}]
          </span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="撞库量" align="center" prop="checkNumber" />
      <el-table-column label="撞库成功" align="center" prop="checkPassNumber" /> -->
      <el-table-column label="撞库量/撞库成功" align="center" prop="channelName">
        <template slot-scope="scope">
          {{ scope.row.checkNumber }} / {{ scope.row.checkPassNumber }}
        </template>
      </el-table-column>

      <el-table-column label="撞库成功率" align="center" prop="zhuangkuRate">
        <template slot-scope="{ row }">
          <span style="color: red">{{ row.zhuangkuRate }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="注册量" align="center" prop="registerNumber" />
      <el-table-column label="注册成功" align="center" prop="registerPassNumber" /> -->

      <el-table-column label="注册量/注册成功" align="center" prop="channelName">
        <template slot-scope="scope">
          {{ scope.row.registerNumber }} / {{ scope.row.registerPassNumber }}
        </template>
      </el-table-column>
      <el-table-column label="注册率" align="center" prop="zhuceRate">
        <template slot-scope="{ row }">
          <span style="color: red">{{ row.zhuceRate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打开页面" align="center" prop="openApproveNumber" />
      <el-table-column label="打开率" align="center" prop="dakaiRate">
        <template slot-scope="{ row }">
          <span style="color: red">{{ row.dakaiRate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="点击推送" align="center" prop="clickApproveNumber" />
      <el-table-column label="点击率" align="center" prop="dianjiRate">
        <template slot-scope="{ row }">
          <span style="color: red">{{ row.dianjiRate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="授权成功/应结算" align="center" prop="approveSuccessNumber">
        <template slot-scope="scope">
          {{ scope.row.approveSuccessNumber }} [* {{ scope.row.settleRate }} = {{ scope.row.shouldNoticeNumber }}]
        </template>
      </el-table-column>
      <el-table-column label="成功率" align="center" prop="successRate">
        <template slot-scope="{ row }">
          <span style="color: red">{{ row.successRate }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="通知数量" align="center" prop="noticeNumber" /> -->
      <el-table-column label="通知数量/通知成功" align="center" prop="noticeSuccessNumber">
        <template slot-scope="scope">
          {{ scope.row.noticeNumber }} / {{ scope.row.noticeSuccessNumber }}
        </template>
      </el-table-column>

    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { listDataCount, getDataCount, delDataCount, addDataCount, updateDataCount } from "@/api/source/dataCount";
import { baseInfoExtData } from "@/api/source/baseInfo";
export default {
  name: "DataCount",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      selectedChannelNames: [],
      // 总条数
      total: 0,
      // 线索数据统计表格数据
      dataCountList: [],
      extData: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateInfo: this.getDefaultDate(),
        channelCode: null,
        channelName: null,
        checkNumber: null,
        checkPassNumber: null,
        registerNumber: null,
        registerPassNumber: null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        dateInfo: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        channelCode: [
          { required: true, message: "渠道编号不能为空", trigger: "blur" }
        ],
        channelName: [
          { required: true, message: "渠道名称不能为空", trigger: "blur" }
        ],
        checkNumber: [
          { required: true, message: "撞库数量不能为空", trigger: "blur" }
        ],
        checkPassNumber: [
          { required: true, message: "撞库通过不能为空", trigger: "blur" }
        ],
        registerNumber: [
          { required: true, message: "注册数量不能为空", trigger: "blur" }
        ],
        registerPassNumber: [
          { required: true, message: "注册通过数量不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getOptions();
  },
  mounted() {

  },
  methods: {
    /** 查询线索基本信息列表 */
    getList() {
      this.loading = true;
      listDataCount(this.queryParams).then(response => {
        this.dataCountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    updateChannelNames() {
      this.queryParams.channelName = this.selectedChannelNames.join(',');
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dateInfo: null,
        channelCode: null,
        channelName: null,
        checkNumber: null,
        checkPassNumber: null,
        registerNumber: null,
        registerPassNumber: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    getDefaultDate() {
      // 获取当天日期，并以 yyyy-MM-dd 格式返回
      const today = new Date();
      const year = today.getFullYear();
      const month = String(today.getMonth() + 1).padStart(2, '0');
      const day = String(today.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.dateInfo = null;
      this.handleQuery();
    },
    getOptions() {
      baseInfoExtData().then((response) => {
        this.extData = response.data;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加线索数据统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDataCount(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改线索数据统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDataCount(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDataCount(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除线索数据统计编号为"' + ids + '"的数据项？').then(function () {
        return delDataCount(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('source/dataCount/export', {
        ...this.queryParams
      }, `dataCount_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
